package com.toocms.jinjuhang.dialog.save_receiver_address

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.databinding.DialogSaveReceiverAddressBinding

class SaveReceiverAddressDialog : DialogFragment() {
    var binding: DialogSaveReceiverAddressBinding? = null
    var viewModel: SaveReceiverAddressModel? = null
    var address: String? = null
        set(value) {
            field = value
            viewModel?.addressObservableField?.set(value)
        }
    var onPassiveClickListener: ((tag: String?) -> Unit)? = null
    var onPositiveClickListener: ((tag: String?) -> Unit)? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.dialog_save_receiver_address,
            container,
            false
        )
        viewModel = SaveReceiverAddressModel(this)
        viewModel?.addressObservableField?.set(address)
        binding!!.setVariable(BR.saveReceiverAddressModel, viewModel)
        return binding!!.root
    }


    override fun onStart() {
        super.onStart()
        dialog?.run {
            setCancelable(true)
            setCanceledOnTouchOutside(true)
            window
        }?.apply {
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            setGravity(Gravity.CENTER)
            setLayout(
                (displayMetrics().widthPixels.toFloat() * 0.75f).toInt(),
                WindowManager.LayoutParams.WRAP_CONTENT
            )
        }
    }

    private fun displayMetrics() = resources.displayMetrics

}